#! /bin/bash

script_dir=$(cd "$(dirname "$0")"; pwd)

build_type=$1
echo $build_type
if [ -z "$build_type" ]; then
    build_type="Release"
fi
conan_cache_dir=${script_dir}/.conan
if [ ! -d "$conan_cache_dir" ] ;then
    mkdir ${conan_cache_dir}
fi

TARGET_VERSION="2.28"

is_old_machine=1
# 获取 glibc 版本号（如 "glibc 2.28"）
GLIBC_INFO=$(getconf GNU_LIBC_VERSION)

# 提取版本号（如 "2.28"）
GLIBC_VERSION=$(echo "$GLIBC_INFO" | awk '{print $NF}')

if printf '%s\n%s' "$GLIBC_VERSION" "$TARGET_VERSION" | sort -V -C; then
    if [ "$(printf '%s\n%s' "$GLIBC_VERSION" "$TARGET_VERSION" | sort -V | head -n1)" != "$TARGET_VERSION" ]; then
        echo "glibc 版本 ($GLIBC_VERSION) 小于 $TARGET_VERSION"
    else
        echo "⚠️ glibc 版本 ($GLIBC_VERSION) 等于 $TARGET_VERSION"
    fi
    is_old_machine=0
else
    echo "glibc 版本 ($GLIBC_VERSION) 大于 $TARGET_VERSION"
fi

# 判断
if [ "$is_old_machine" -eq 1 ]; then
    cp ${script_dir}/conanfile.txt ${conan_cache_dir}/conanfile.txt
else
    cp ${script_dir}/conanfile_legend.txt ${conan_cache_dir}/conanfile.txt
fi


cd ${conan_cache_dir}
conan install . -of=$build_type -b=missing -s build_type=$build_type -d direct_deploy